<?php

namespace App\Http\Controllers\Admin;

use App\Product;
use App\Type;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ProductController extends Controller
{
    public function index(Request $request)
    {
        $products = Product::orderBy('updated_at', 'desc')->get();
        return view('admin.product.index', [
            'request' => $request,
            'products' => $products
        ]);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'factory' => 'required',
                'model' => 'required',
                'name' => 'required',
                'thumbnail' => 'required'
            ];
            $messages = [
                'factory.required' => '请选择车厂！',
                'model.required' => '请选择车型！',
                'name.required' => '请输入产品名！',
                'thumbnail.required' => '请上传产品展示图！'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'code' => __LINE__,
                    'msg' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $files = '';
            $fileNames = '';
            for ($i = 1; $i <= 5; $i++) {
                $fileNames .= $data['attachmentName'.$i] . ',';
                $files .= $data['attachment'.$i] . ',';
            }
            $product = new Product();
            $product->factory_id = $data['factory'];
            $product->model_id = $data['model'];
            $product->name = $data['name'];
            $product->thumbnail = rtrim($data['thumbnail'], ',');
            $product->file_names = rtrim($fileNames, ',');
            $product->files = rtrim($files, ',');
            $product->content = $data['content'];
            $product->save();
            $return = ['code' => 0];
            return response()->json($return);
        }
        return view('admin.product.add', [
            'request' => $request
        ]);
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'factory' => 'required',
                'model' => 'required',
                'name' => 'required'
            ];
            $messages = [
                'factory.required' => '请选择车厂！',
                'model.required' => '请选择车型！',
                'name.required' => '请输入产品名！'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'code' => __LINE__,
                    'msg' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $files = '';
            $fileNames = '';
            $product = Product::find($data['id']);
            $product->factory_id = $data['factory'];
            $product->model_id = $data['model'];
            $product->name = $data['name'];
            if (!empty($data['thumbnail'])) {
                $product->thumbnail = rtrim($data['thumbnail'], ',');
            }
            for ($i = 1; $i <= 5; $i++) {
                $fileNames .= $data['attachmentName'.$i] . ',';
                if (empty($data['attachment'.$i])) {
                    $files .= $product->files[$i-1] . ',';
                } else {
                    $files .= $data['attachment'.$i] . ',';
                }
            }
            $product->file_names = rtrim($fileNames, ',');
            $product->files = rtrim($files, ',');
            $product->content = $data['content'];
            $product->save();
            $return = ['code' => 0];
            return response()->json($return);
        }
        $product = Product::find($id);
        $models = Type::where('parent_id', '=', $product->factory_id)->get();
        return view('admin.product.edit', [
            'request' => $request,
            'models' => $models,
            'product' => $product
        ]);
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        $product = Product::find($id);
        if ($product->delete()) {
            $return = ['code' => 0];
        } else {
            $return = ['code' => __LINE__, 'msg' => '删除失败'];
        }
        return response()->json($return);
    }
}